package com.itHuangTao.tank.ui;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.texture.Texture;
import com.itHuangTao.tank.Config;
import com.itHuangTao.tank.GameType;
import com.itHuangTao.tank.TankApp;
import com.itHuangTao.tank.components.TankLevelComponent;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.layout.HBox;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

//创建一个信息面板
public class InfoPane extends VBox {
    public InfoPane() {
        //1.设置坦克信息
        //设置间距
        setSpacing(20);
        //设置背景颜色
        setStyle("-fx-background-color: lightgray");
        //设置宽高
        setPrefSize(6 * Config.CELL_SIZE, FXGL.getAppHeight());
        //设置偏移量
        setTranslateX(28 * Config.CELL_SIZE);
        //设置居中对齐
        setAlignment(Pos.TOP_CENTER);

        TilePane tp = new TilePane();
        //设置外边距(上边)
        VBox.setMargin(tp, new Insets(30, 0, 0, 0));
        //将这些坦克信息图片对齐
        tp.setTileAlignment(Pos.CENTER);
        //设置水平间隔
        tp.setHgap(10);
        //设置垂直间距
        tp.setVgap(8);
        //设置最大间隔宽度
        tp.setMaxWidth(140);
        //设置首选高度
        tp.prefHeight(25 * 10 + 6 * 10 + 15);
        for (int i = 0; i < TankApp.maxEnemyAmount; i++) {
            //每循环一次添加一个图片
            tp.getChildren().add(FXGL.texture("enemy_pre.png"));
        }
        getChildren().add(tp);

        //2.设置关卡信息
        Texture texture = FXGL.texture("levelFlag.png");
        Text text = new Text("" + FXGL.geti("level"));
        text.setFont(Font.font(26));

        //关卡对应名称
        Text textLevel = TextMethod(FXGL.geti("level"));
        textLevel.setFont(Font.font(26));
        textLevel.setFill(Color.RED);

        HBox levelBox = new HBox(texture, text);
        HBox levelNameBox = new HBox(textLevel);
        levelBox.setMaxWidth(70);
        levelBox.setAlignment(Pos.BOTTOM_CENTER);
        levelNameBox.setMaxWidth(70);
        levelNameBox.setAlignment(Pos.BOTTOM_CENTER);
        getChildren().addAll(levelBox, levelNameBox);

        //3.设置子弹等级信息
        HBox bulletBox = new HBox();
        bulletBox.setMaxWidth(29 * 3 + 5);
        for (int i = 0; i <= Config.TANK_MAX_LEVEL; i++) {
            Texture tt = FXGL.texture("bulletLevel.png");
            tt.setVisible(i == 0);
            bulletBox.getChildren().add(tt);
        }
        getChildren().add(bulletBox);

        Entity entity = FXGL.getGameWorld().getEntitiesByType(GameType.PLAYER).get(0);
        entity.getComponent(TankLevelComponent.class).valueProperty().addListener((ob, ov, nv) -> {
            for (int i = 0; i <= nv.intValue(); i++) {
                bulletBox.getChildren().get(i).setVisible(true);
            }
        });


        //当每产生一个敌方坦克就移除一个坦克信息
        FXGL.getip("spawnedEnemyAmount").addListener((ob, ov, nv) -> {
            //获取当前子节点
            ObservableList<Node> nodes = tp.getChildren();
            for (int i = nodes.size() - 1; i >= TankApp.maxEnemyAmount - nv.intValue(); i--) {
                //使得一个坦克信息不显示
                nodes.get(i).setVisible(false);
            }
        });
    }

    public Text TextMethod(int level) {
        if (level == 1) {
            return new Text("初次相见");
        } else if (level == 2) {
            return new Text("热血街头");
        } else if (level == 3) {
            return new Text("黑暗丛林");
        } else if (level == 4) {
            return new Text("岛屿奇兵");
        } else if (level == 5) {
            return new Text("未知恐惧");
        } else if (level == 6) {
            return new Text("护城战争");
        } else if (level == 7) {
            return new Text("石门庄");
        } else if (level == 8) {
            return new Text("决战威尼斯");
        }
        return new Text("Virtory");
    }
}
